/**
 * Copyright (c) 2019-2029, wwww.kiwipeach.cn (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cn.kiwipeach.blog.controller;


import cn.kiwipeach.blog.anno.CurrentUser;
import cn.kiwipeach.blog.base.AjaxResponse;
import cn.kiwipeach.blog.database.domain.BlogCommentVO;
import cn.kiwipeach.blog.param.CommentReplyParam;
import cn.kiwipeach.blog.service.ICommentReplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import open.kiwipeach.autoconfigure.shiro.token.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 评论 前端控制器
 *
 * @author kiwipeach
 * @since 1.0
 */
@Api(tags = "博客评论回复")
@Controller
@RequestMapping("/commentReply")
public class CommentReplyController {

    @Autowired
    private ICommentReplyService iCommentReplyService;


    /**
     * 发表博客评论
     *
     * @param commentReply 评论对象
     * @param accessToken  当前登录用户
     * @return 返回评论结果
     */
    @PostMapping("comment/add")
    @ResponseBody
    public AjaxResponse<BlogCommentVO> createBlogComment(
            @RequestBody CommentReplyParam commentReply,
            @CurrentUser AccessToken accessToken) {
        return iCommentReplyService.createBlogComment(commentReply, accessToken);
    }


    /**
     * 博客评论回复
     *
     * @param commentReply 评论对象
     * @param accessToken  当前登录用户
     * @return
     */
    @PostMapping("reply/add")
    @ResponseBody
    public AjaxResponse<BlogCommentVO> createCommentReply(
            @RequestBody CommentReplyParam commentReply,
            @CurrentUser AccessToken accessToken) {
        return iCommentReplyService.createCommentReply(commentReply, accessToken);
    }


    /**
     * 评论回复分页查询接口
     *
     * @param page      分页入参
     * @param queryType 查询类型,[B_BLOG_COMMENT,B_COMMENT_REPLY,B_REPLY_REPLY,L_COMMENT,L_COMMENT_REPLY,L_REPLY_REPLY]
     * @param parentId  父节点编号
     * @return 返回分页结果
     */
    @GetMapping("query")
    @ResponseBody
    public AjaxResponse<IPage<BlogCommentVO>> pageQuery(Page page,
                                                        @RequestParam(required = true, value = "queryType") String queryType,
                                                        @RequestParam(required = true, value = "parentId") String parentId) {
        return iCommentReplyService.queryCommentByPage(page, parentId, queryType);
    }


    /**
     * 评论回复点赞接口
     *
     * @param commentId 评论编号
     * @return 返回博客点赞结果
     */
    @PostMapping(value = "agree/{commentId}")
    @ResponseBody
    public AjaxResponse<Boolean> makeBlogAgree(@PathVariable("commentId") String commentId, HttpServletRequest request) {
        return iCommentReplyService.modifyCommentReplyStarCount(commentId, request.getSession().getId());
    }


}
